'use strict'
const moment = require('moment')
const Sequelize = require('sequelize')
module.exports = {
  schedule: {
    interval: '6h', // 5 min间隔
    type: 'worker', // worker 类型：每台机器上只有一个 worker 会执行这个定时任务
  },
  async task(ctx) {
    const { rider, scheduleResetScore, riderXf } = ctx.service
    const { Op } = Sequelize

    const quarterArr = ['01-01', '04-01', '07-01', '10-01']
    const currentDate = moment()
    const currentFormat = currentDate.format('MM-DD')
    const isResetDay = quarterArr.indexOf(currentFormat)
    // 如果是重置分数日
    if (isResetDay > -1) {
      const hasData = await scheduleResetScore.findOne({
        where: {
          reset_time: {
            [Op.between]: [
              currentDate.format('YYYY-MM-DD 00:00:00'),
              currentDate.format('YYYY-MM-DD 23:59:59'),
            ],
          },
        },
      })
      if (!hasData) {
        const newRiderInfo = await rider.update(
          { wz_score: 12, status: 'GREEN' },
          { status: { [Op.in]: ['RED', 'ORANGE', 'GREEN'] } }
        )
        await riderXf.update({ report_count: 0, exam_count: 0 }, {})
        let rider_count = 0
        if (newRiderInfo.length > 0) {
          rider_count = newRiderInfo[0]
        }
        await scheduleResetScore.create({
          reset_time: moment().format('YYYY-MM-DD HH:mm:ss'),
          rider_count,
        })
      }
    }
  },
}
